package swu.edu.cn.javaee.controller;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import swu.edu.cn.javaee.entity.CollectInfo;
import swu.edu.cn.javaee.service.CollectService;
import static swu.edu.cn.javaee.component.Signal.*;



@Controller
//解决跨域问题：允许所有源域名访问
@CrossOrigin(origins = "*")

public class CollectController {
    @Resource
    private CollectService collectService;
    //文章收藏
    @RequestMapping("/articleCollect")
    @ResponseBody
    public String articleCollect(String user_id,String article_id){
        String result= collectService.articleCollect(user_id,article_id);
        if (result!=null){
            return result;
        }
        else {
            return FAILED_CODE_Article_NO_EXIST;
        }
    }

    //评论收藏
    @RequestMapping("/commentCollect")
    @ResponseBody
    public String reviewCollect(String user_id,String review_id){
        String result= collectService.reviewCollect(user_id,review_id);
        if (result!=null){
            return result;
        }
        else {
            return FAILED_CODE_NO_COMMENT;
        }
    }

    //商户收藏
    @RequestMapping("/merchantCollect")
    @ResponseBody
    public String merchantCollect(String user_id,String merchant_id){
        String result= collectService.merchantCollect(user_id,merchant_id);
        if (result!=null){
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_MERCHANT_NO_EXIST;
        }
    }

    //商品收藏
    @RequestMapping("/productCollect")
    @ResponseBody
    public String productCollect(String user_id,String product_id){
        String result= collectService.productCollect(user_id,product_id);
        if (result!=null){
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_PRODUCT_NO_EXIST;
        }
    }

    //展示用户文章收藏
    @RequestMapping("/queryCollectArticle")
    @ResponseBody
    public CollectInfo queryArticleList(String user_id){
        CollectInfo collectInfo=collectService.queryArticleList(user_id);
        if (collectInfo!=null){
            System.out.println(collectInfo);
            return collectInfo;
        }
        else {
            return null;
        }
    }

    //展示用户评论收藏
    @RequestMapping("/queryCollectReview")
    @ResponseBody
    public CollectInfo queryReviewList(String user_id){
        CollectInfo collectInfo=collectService.queryReviewList(user_id);
        if (collectInfo!=null){
            System.out.println(collectInfo);
            return collectInfo;
        }
        else {
            return null;
        }
    }

    //展示用户商户收藏
    @RequestMapping("/queryCollectMerchant")
    @ResponseBody
    public CollectInfo queryMerchantList(String user_id){
        CollectInfo collectInfo=collectService.queryMerchantList(user_id);
        if (collectInfo!=null){
            System.out.println(collectInfo);
            return collectInfo;
        }
        else {
            return null;
        }
    }

    //展示用户商品收藏
    @RequestMapping("/queryCollectProduct")
    @ResponseBody
    public CollectInfo queryProductList(String user_id){
        CollectInfo collectInfo=collectService.queryProductList(user_id);
        if (collectInfo!=null){
            System.out.println(collectInfo);
            return collectInfo;
        }
        else {
            return null;
        }
    }

    //删除文章收藏
    @RequestMapping("/deleteArticleCollect")
    @ResponseBody
    public String deleteArticle(String user_id,String article_id){
        String result=collectService.deleteArticle(user_id,article_id);
        if (result==SUCCESS_CODE){
            return SUCCESS_CODE_DELETE;
        }
        else {
            return FAILED_CODE_DELETE;
        }
    }

    //删除评论收藏
    @RequestMapping("/deleteReviewCollect")
    @ResponseBody
    public String deleteReview(String user_id,String review_id){
        String result=collectService.deleteReview(user_id,review_id);
        if (result==SUCCESS_CODE){
            return SUCCESS_CODE_DELETE;
        }
        else {
            return FAILED_CODE_DELETE;
        }
    }

    //删除商户收藏
    @RequestMapping("/deleteMerchantCollect")
    @ResponseBody
    public String deleteMerchant(String user_id,String merchant_id){
        String result=collectService.deleteMerchant(user_id,merchant_id);
        if (result==SUCCESS_CODE){
            return SUCCESS_CODE_DELETE;
        }
        else {
            return FAILED_CODE_DELETE;
        }
    }
    //删除商品收藏
    @RequestMapping("/deleteProductCollect")
    @ResponseBody
    public String deleteProduct(String user_id,String product_id){
        String result=collectService.deleteProduct(user_id,product_id);
        if (result==SUCCESS_CODE){
            return SUCCESS_CODE_DELETE;
        }
        else {
            return FAILED_CODE_DELETE;
        }
    }

}
